package org.hsieh.tr.explore.common.ui;

import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;
import org.hsieh.tr.explore.annotation.AnnotationParser;
import org.hsieh.tr.explore.annotation.TreeNodeLabel;
import org.hsieh.tr.explore.ui.ResourceHandler;

public class AnnotationLabelProvider implements ILabelProvider {

    public Image getImage(Object element) {
	if (element != null) {
	    return getAnnotationImage(element);

	}
	return null;
    }

    public String getText(Object element) {
	if (element != null) {
	    return getLabelText(element);
	}
	return null;
    }

    public void addListener(ILabelProviderListener listener) {

    }

    public void dispose() {

    }

    public boolean isLabelProperty(Object element, String property) {
	return false;
    }

    public void removeListener(ILabelProviderListener listener) {

    }

    private Image getAnnotationImage(Object obj) {
	TreeNodeLabel treeImage = AnnotationParser.findAnnotation(obj,
		TreeNodeLabel.class);
	if (treeImage != null)
	    return ResourceHandler.getInstance().getImage(treeImage.image());
	return null;
    }

    private String getLabelText(Object obj) {
	Object value = AnnotationParser.getValueAnnotationWith(obj,
		TreeNodeLabel.class);
	if (value != null)
	    return value.toString();
	return obj.toString();
    }

}
